﻿/*
 * Copyright © 2025−2026 WEI.ZHOU (Willis) (Chengdu). All rights reserved.
 *
 * THIS SOURCE CODE IS PROPRIETARY AND CONFIDENTIAL.
 * It is the exclusive property of WEI.ZHOU (Willis) and may not be
 * used, copied, distributed, or disclosed outside of WEI.ZHOU (Willis)
 * without express written permission.
 *
 * This code is provided to WEI.ZHOU (Willis) under a limited, 
 * non-transferable internal license, and is for use only with the author's 
 * approved purposes. No external license is granted or implied.
 *
 * Any unauthorized use, reproduction, or distribution is strictly prohibited
 * and may result in severe civil and criminal penalties.
 */
 
using Aore.Core.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Aore.Core.Extensions;

/// <summary>
/// 添加DbContext
/// </summary>
public static class EfCoreDbContextExtensions
{
  
  public static void AddDbContext<T>(this IServiceCollection services, 
    IConfiguration configuration, 
    string datebaseType = "Mysql") 
    where T : DbContext
  {
    if(DatabaseType.MySql.ToString().ToUpper().Equals(datebaseType.ToUpper()))
    {
      services.AddDbContext<T>(options =>
        options.UseMySQL(configuration[$"DatabaseConfig:ConnectionStrings:{datebaseType}"] ?? string.Empty));
      return;
    }
    
    
    if(DatabaseType.Oracle.ToString().ToUpper().Equals(datebaseType.ToUpper()))
    {
      services.AddDbContext<T>(options =>
        options.UseOracle(configuration[$"DatabaseConfig:ConnectionStrings:{datebaseType}"] ?? string.Empty));
      return;
    }
    
    if(DatabaseType.Oracle.ToString().ToUpper().Equals(datebaseType.ToUpper()))
    {
      services.AddDbContext<T>(options =>
        options.UseSqlServer(configuration[$"DatabaseConfig:ConnectionStrings:{datebaseType}"] ?? string.Empty));
      return;
    }
  }
    
  /// <summary>
  /// 或者更简洁的版本，直接使用默认连接字符串名称
  /// </summary>
  /// <param name="services"></param>
  /// <param name="configuration"></param>
  /// <typeparam name="T"></typeparam>
  /// <exception cref="ArgumentException"></exception>
  public static void AddDbContext<T>(this IServiceCollection services, IConfiguration configuration) 
    where T : DbContext
  {
    var databaseType = configuration["DatabaseConfig:DatabaseProvider"];
    
    if (string.IsNullOrEmpty(databaseType))
    {
      throw new ArgumentException("DatabaseProvider is not set.");
    }
    
    if(DatabaseType.MySql.ToString().ToUpper().Equals(databaseType?.ToUpper()))
    {
      services.AddDbContext<T>(options =>
        options.UseMySQL(configuration[$"DatabaseConfig:ConnectionStrings:{databaseType}"] ?? string.Empty));
      return;
    }
    
    if(DatabaseType.Oracle.ToString().ToUpper().Equals(databaseType?.ToUpper()))
    {
      services.AddDbContext<T>(options =>
        options.UseOracle(configuration[$"DatabaseConfig:ConnectionStrings:{databaseType}"] ?? string.Empty));
      return;
    }
    
    if(DatabaseType.SqlServer.ToString().ToUpper().Equals(databaseType?.ToUpper()))
    {
      services.AddDbContext<T>(options =>
        options.UseSqlServer(configuration[$"DatabaseConfig:ConnectionStrings:{databaseType}"] ?? string.Empty));
      return;
    }
  }
}